package com.kanqiubao.kq.activity.recoding;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import com.kanqiubao.kq.R;
import com.kanqiubao.kq.activity.BaseActivity;
import com.kanqiubao.kq.utils.AppConfig;
import com.kanqiubao.kq.utils.DebugUntil;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.tencent.smtt.sdk.WebView;

public class WebviewActivity extends BaseActivity<String> {
    private WebView mWebView;
    private String mUrl;

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        mWebView = findView(R.id.webview_id);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mUrl = "http://m.miguvideo.com/mgs/msite/prd/detail.html?cid=691422730";
        ((EditText)findView(R.id.edit_id)).setText(mUrl);
        mWebView.loadUrl(mUrl);
        Bundle data = new Bundle();
        data.putBoolean("standardFullScreen", false);// true表示标准全屏，会调起onShowCustomView()，false表示X5全屏；不设置默认false，
        data.putBoolean("supportLiteWnd", false);// false：关闭小窗；true：开启小窗；不设置默认true，
        data.putInt("DefaultVideoScreen", 1);// 1：以页面内开始播放，2：以全屏开始播放；不设置默认：1
        mWebView.getX5WebViewExtension().invokeMiscMethod("setVideoParams", data);
    }

    @Override
    protected int getActivitylayout() {
        return R.layout.recoding_detail_webview_id;
    }

    @Override
    protected String getTitleText() {
        return null;
    }

    /*处理WebView返回事件*/
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_BACK) {
            if (mWebView.canGoBack()) {
                mWebView.goBack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void click(View v) {
        switch (v.getId()) {
            case R.id.refresh:
                mWebView.reload();
                break;
            case R.id.more:
                more(v);
                break;
        }
        super.click(v);
    }

    private void more(View v) {
        new XPopup.Builder(this)
                .hasShadowBg(false)
                .atView(v)  // 依附于所点击的View，内部会自动判断在上方或者下方显示
                .asAttachList(new String[]{"复制链接", "分享链接", "浏览器打开"},
                        new int[]{R.mipmap.icon_copy_url, R.mipmap.icon_share2, R.mipmap.icon_browser},
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                if (position == 0) {
                                    copy();
                                    DebugUntil.createInstance().toastStr("复制成功");
                                } else if (position == 1) {
                                    shared();
                                } else {
                                    AppConfig.startWebView(mUrl, WebviewActivity.this);
                                }
                            }
                        }, 0, 0).show();
    }

    private void copy() {
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("Label", mUrl);
// 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
    }

    private void shared() {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, mUrl);
        //切记需要使用Intent.createChooser，否则会出现别样的应用选择框，您可以试试
        shareIntent = Intent.createChooser(shareIntent, "");
        startActivity(shareIntent);
    }
}
